import tkinter
from interface.widget import PagePort, scale_factor
from interface.pages.schedule import SchedulePage
from interface.pages.bank import BankerPage
import ctypes
from utils import memory


class Application:
    def __init__(self):
        try:  # Try to disable high dpi auto adjust on Windows.
            ctypes.windll.shcore.SetProcessDpiAwareness(1)
        except AttributeError:  # Linux
            pass
        except OSError:  # Happens on some old computer.
            pass

        self.page_port = None
        self.tk = tkinter.Tk()
        self.tk.geometry('{}x{}'.format(int(800 * scale_factor), int(600 * scale_factor)))
        self.tk.title('进程调度模拟程序')
        memory.app = self

    def content(self, master):
        page_port = PagePort(master)
        menubar = tkinter.Menu(master)

        def switch_page(page_type):
            page_port.close_page()
            page_port.open_page(page_type(page_port))

        menubar.add_command(label='进程调度', command=lambda: switch_page(SchedulePage))
        menubar.add_command(label='银行家算法', command=lambda: switch_page(BankerPage))
        self.tk.config(menu=menubar)
        page_port.relwidth = 1
        page_port.relheight = 1
        page_port.display()
        page_port.open_page(SchedulePage(page_port))
        self.page_port = page_port

    def mainloop(self):
        self.content(self.tk)
        self.tk.mainloop()
